package MyStack;

import java.util.Arrays;

/**
 * Description:
 * Date: 2025-11-10
 * Time: 14:44
 */
public class MyStack {
    private int[] array;
    private int usedSize;

    public MyStack() {
        array = new int[10];
    }

    public void push(int data) {
        //java中底层是一个动态的顺序表
        if (isFull()) {
            grow();
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }

    private boolean isFull() {
        return this.usedSize == array.length;
    }

    private void grow() {
        this.array = Arrays.copyOf(this.array,2 * this.array.length);
    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        int val = this.array[this.usedSize-1];
        this.usedSize--;
        return val;
    }

    public int peek() {
        if (empty()) {
            return -1;
        }
        return this.array[this.usedSize-1];
    }

    public int size() {
        return this.usedSize;
    }

    public boolean empty() {
        return this.usedSize == 0;
    }

    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }

}
